/**
 * 自定义 token 操作
 * @author LiQingSong
 */
import localforage from 'localforage';
import settings from '@/config/settings';

/**
 * 获取本地Token
 */
// export const getToken = async (): Promise<string | null> => {
//   return await localforage.getItem(settings.siteTokenKey);
// };
export const getToken = async (): Promise<string | null> => {
  const json = localStorage.getItem(settings.siteTokenKey);
  if (json) {
    let storageData = null;

    try {
      storageData = JSON.parse(json);
    } catch {
      // TODO
    }

    if (storageData) {
      return storageData;
    }
  }
  return null
};

/**
 * 设置存储本地Token
 */
// export const setToken = async (token: string): Promise<boolean> => {
//   try {
//     await localforage.setItem(settings.siteTokenKey, token);
//     return true;
//   } catch (error) {
//     return false;
//   }
// };
export const setToken = async (token: string): Promise<boolean> => {
  try {
    localStorage.setItem(settings.siteTokenKey, token);
    return true;
  } catch (error) {
    return false;
  }
};

/**
 * 移除本地Token
 */
// export const removeToken = async (): Promise<boolean> => {
//   try {
//     await localforage.removeItem(settings.siteTokenKey);
//     return true;
//   } catch (error) {
//     return false;
//   }
// };
export const removeToken = async (): Promise<boolean> => {
  try {
    localStorage.removeItem(settings.siteTokenKey);
    return true;
  } catch (error) {
    return false;
  }
};